package com.dykj.scheduling;

import com.HttpUtils;
import com.alibaba.fastjson.JSONObject;
import com.dykj.dao.CustinfoMapper;
import com.dykj.dao.SalesorderinfoMapper;
import com.dykj.dao.SupplierbasicinfoMapper;
import com.dykj.entity.Custinfo;
import com.dykj.entity.Salesorderinfo;
import com.dykj.entity.Supplierbasicinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LALA
 * Date: 2018-11-26
 * Time: 10:07
 */
@Component
public class QuartzService {

    public final static String ONE_DAY = "0 0 3 * * ?  ";

    @Autowired
    private CustinfoMapper custinfoMapper;

    @Autowired
    private SupplierbasicinfoMapper supplierbasicinfoMapper;

    @Autowired
    private SalesorderinfoMapper salesorderinfoMapper;
    //0 0 3 * * ? *每天凌晨3点启动一次
    @Scheduled(cron = ONE_DAY)
    public List<Salesorderinfo> timerToNow(){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE,-2);
        Calendar calendar2 = new GregorianCalendar();
        calendar2.setTime(new Date());
        calendar2.add(calendar.DATE,-1);
        String start = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        String end = new SimpleDateFormat("yyyy-MM-dd").format(calendar2.getTime());

        System.out.println(start+"/"+end+new Date());

        String url = "http://192.168.2.129:666/saleOrder/"+start+"/"+end;
        String json = HttpUtils.get(url,null);
        //是不是可能是这个/位置错了
        List<Salesorderinfo> list = JSONObject.parseArray(json,Salesorderinfo.class);
       // String start=  new SimpleDateFormat("yyyy-MM-dd ").format(new Date());
        //String end=  new SimpleDateFormat("yyyy-MM-dd+1 ").format(new Date());
        return list;

    }

    @Scheduled(cron = ONE_DAY)
    public void nextSupploerbasicinfo(){
        supplierbasicinfoMapper.deleteByPrimaryKey();
        System.out.println("删除Supploerbasicinfo数据成功"+new Date());
        String json = HttpUtils.get("http://192.168.2.129:666/supplier",null);
        List<Supplierbasicinfo> list = JSONObject.parseArray(json,Supplierbasicinfo.class);
        for(Supplierbasicinfo Supplierbasicinfo : list){
            this.supplierbasicinfoMapper.insertSelective(Supplierbasicinfo);
        }
        System.out.println("添加Supploerbasicinfo"+list.size());

    }
    //cron = "0/59 * * * * ?  "表示59秒运行一次
    @Scheduled(cron = ONE_DAY)
    public void nextCustom(){
        custinfoMapper.deleteByPrimaryKey();
        System.out.println("删除custom数据成功"+new Date());
        String json = HttpUtils.get("http://192.168.2.129:666/custom",null);
        List<Custinfo> list = JSONObject.parseArray(json,Custinfo.class);
        int size = list.size();
        for(Custinfo Custinfo : list){
            this.custinfoMapper.insertSelective(Custinfo);
        }
        System.out.println("添加custom"+list.size());

    }

}
